﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Badger.Vhdl.Types;
using Weazel.Badger.Vhdl.Expressions;

#endregion

namespace Weazel.Badger.Vhdl.Statements
{
  public class WaitStatement : SequentialStatement
  {
    private int nanoseconds = 0;
    private Expression expression;

    public WaitStatement()
    {
      // wait .. 
    }

    public WaitStatement(int nanoseconds)
    {
      if (nanoseconds < 1)
        throw new Exception("unacceptable wait period");

      this.nanoseconds = nanoseconds;
    }

    public WaitStatement(Expression expression)
    {
      this.expression = expression;
    }

		public override void Resolve(Entity entity, Process process, StatementCollection statements)
    {
      if (expression != null)
      {
        TypeSet types = this.expression.GetPossibleTypes();
        System.Diagnostics.Debug.Assert(types.Contains(Types.Types.Boolean));
				this.expression.SetActualType(entity, process, statements, new Types.Boolean());
      }
    }

    public override void Verify(ref List<VerifyMessage> messages)
    {
     // nothing
    }

    public override void Write(System.CodeDom.Compiler.IndentedTextWriter writer)
    {
      if (expression != null)
        writer.WriteLine("wait until {0};", expression.ToString());
      else if (nanoseconds == 0)
        writer.WriteLine("wait;");
      else
        writer.WriteLine("wait for {0} ns;", nanoseconds.ToString());
    }
  }
}
